var _poolModule = require('generic-pool');
var mysql = require('mysql');
/*
 * Create mysql connection pool.
 */
var createMysqlPool = function(app) {
	var mysqlConfig = app.get('mysql');
	const factory = {
		create:function(){
			return new Promise(function(resolve,reject){
				var client = mysql.createPool({
					host: mysqlConfig.host,
					user: mysqlConfig.user,
					password: mysqlConfig.password,
					database: mysqlConfig.database
				});
				resolve(client);
			})
		},
		destroy: function(client) {
			return new Promise(function(resolve){
				client.end();
				resolve();
			})
		},
	}
	var opts = {
		max:10,
		min:2,
		idleTimeoutMillis : 30000,
		log:false
	}
	return _poolModule.createPool(factory,opts);
};


exports.createMysqlPool = createMysqlPool;
